package com.yjt.fresh.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyMyScore;
import com.yjt.fresh.api.ApiMyPoints;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanMyPoints;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.nfc.tech.MifareUltralight.PAGE_SIZE;

/**
 * 我的积分
 *
 * @author mcc
 * @date 2018/6/17
 */

public class AtyMyScore extends BaseSimpleActivity {
    @BindView(R.id.rv_app_aty_myscore)
    RecyclerView mRv;
    @BindView(R.id.rlAppAtyMyScoreBack)
    RelativeLayout mRlBack;
    @BindView(R.id.tvAppAtyMyScorePoints)
    TextView mTvPoints;
    @BindView(R.id.srlAppAtyMyScore)
    SwipeRefreshLayout mRefreshLayout;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;
    private AdpRvAtyMyScore mAdpMyScore;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_myscore;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        getData();
    }

    HttpOnNextListener<List<BeanMyPoints>> mPointsListener = new HttpOnNextListener<List<BeanMyPoints>>() {
        @Override
        public void onNext(List<BeanMyPoints> beanMyPoints) {
            mAdpMyScore.setEmptyView(R.layout.view_empty);
            if (beanMyPoints.size() > 0){
                mTvPoints.setText(beanMyPoints.get(0).getPOINTS());
            }
            if (isLoadMore) {
                setData(false, beanMyPoints);
            } else {
                setData(true, beanMyPoints);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanMyPoints> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpMyScore.getData().clear();
            mAdpMyScore.addData(data);
        } else {
            if (size > 0) {
                mAdpMyScore.addData(data);
                mAdpMyScore.loadMoreComplete();
            }else {
                mAdpMyScore.loadMoreEnd(true);
            }
        }

        if (size < 20){
            mAdpMyScore.loadMoreEnd(true);
        }
    }
    private void getData() {
        ApiMyPoints apiMyPoints = new ApiMyPoints(mPointsListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("currentPage",mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiMyPoints.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiMyPoints);
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanMyPoints> list = new ArrayList<>();
        mAdpMyScore = new AdpRvAtyMyScore(R.layout.app_recycle_item_myscore, list);
        mRv.setAdapter(mAdpMyScore);
    }
    private void initClick(){
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpMyScore.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        },mRv);
    }
}
